$(function(){
	//加载数据
	loadDatas();

	//初始化日期控件
  	$('#doingTime').datetimepicker({
  		language: 'zh-CN',
	    format: 'yyyy-mm-dd hh:ii:00',
	    autoclose: true,
	    minView: "hour",
	    minuteStep: 5,
	    startDate: new Date()
	});
	
	//加载游戏列表
	loadGames();
});


//数据加载
function loadDatas(){
	
	//游戏
	var game = $("#game").val()
	//房间名
	var room = $("#room").val();
	//状态
	var state = $("#state").val();
	//输赢
	var winLose = $("#winLose").val();
	
	var roomName = "";
	if(room != ""){
		roomName = room;
	}else{
		if(game!= ""){
			game = game.substring(game.indexOf("/")+1);
			roomName = game;
		}
	}
	
	$.ajax({
		type: "post",
		data: {"room":roomName, "state":state, "winLose":winLose},
		url: "roomSet",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			var datas = msg.records;
			var html = "";
			//表格数据
			if(datas.length==0){
				html += "<tr><td colspan='10'>没查询到符合条件的数据~</td></tr>";
			}else{
				for(var i = 0; i<datas.length; i++){
					html += "<tr>";
					html += "<td>"+datas[i].serverid+"</td>";
					html += "<td>"+datas[i].roomname+"</td>";
					var zsy = datas[i].zsy;
					if(zsy > 0){
						html += "<td><font color='red'>"+zsy+"</font></td>";
					}else{
						html += "<td>"+zsy+"</td>";
					}
					html += "<td>"+datas[i].drsy+"</td>";
					html += "<td>"+datas[i].xcph+"</td>";
					var syxc = datas[i].syxc;
					html += "<td>"+syxc+"</td>";
					var kindId = datas[i].kindId;
					if(kindId == "4"){
						if(syxc < 8000000){
							html += "<td><font color='red'>系统吃分</font></td>";
						}else if(syxc >= 8000000 && syxc <= 12000000){
							html += "<td><font color=''>自然状态</font></td>";
						}else if(syxc > 12000000){
							html += "<td><font color='green'>系统吐分</font></td>";
						}
					}else if(kindId == "6"){
						if(syxc < 20000000){
							html += "<td><font color='red'>系统吃分</font></td>";
						}else if(syxc >= 20000000 && syxc <= 80000000){
							html += "<td><font color=''>自然状态</font></td>";
						}else if(syxc > 80000000){
							html += "<td><font color='green'>系统吐分</font></td>";
						}
					}else{
						var state = datas[i].state;
						if(state=="系统吃分"){
							html += "<td><font color='red'>系统吃分</font></td>";
						}else if(state=="系统吐分"){
							html += "<td><font color='green'>系统吐分</font></td>";
						}
					}
					html += "<td>"+doubleFormat(datas[i].tfl)+"</td>";
					var time = datas[i].time;
					if(time != ""){
						time += "<br/>";
					}
					html += "<td>"+time+datas[i].xcfs+"</td>";
					html += "<td>"+time+datas[i].xcrate+"</td>";
					//操作
//					html += '<td><table class="caozuo" cellpadding="0" cellspacing="0">';
					html += '<td><table class="caozuo" cellpadding="0" cellspacing="0">';
					html += '<tr>';
					html += '<td><a href="javascript:void();" onclick="addXueChi(\''+datas[i].serverid+'\',\''+datas[i].roomname+'\',\''+datas[i].xcph+'\',\''+datas[i].syxc+'\')">添加血池控制</a></td>';
					html += '<td><a href="javascript:void();" target="_blank" onclick="window.location.href=\'xueChiKongZhiList?kindId='+datas[i].kindId+'&serverid='+datas[i].serverid+'\'">血池控制列表</a></td>';
					html += '<td><a href="javascript:void();" target="_blank" onclick="window.location.href=\'historicalThroughput?kindId='+datas[i].kindId+'&serverid='+datas[i].serverid+'\'">历史吞吐率</a></td>';
					html += '<td><a href="javascript:void();" target="_blank" onclick="window.location.href=\'bloodPoolChanges?serverid='+datas[i].serverid+'\'">血池变化</a></td>';
					html += '<td><a href="javascript:void();" onclick="changeXueChi(\''+datas[i].serverid+'\',\''+datas[i].roomname+'\',\''+datas[i].xcrate+'\')">修改血池税率</a></td>';
					html += '</tr>';
//					html += '<tr>';
//					html += '<td><a href="javascript:void();" onclick="window.location.href=\'historicalThroughput.php?kindId='+datas[i].kindId+'&serverid='+datas[i].serverid+'\'">历史吞吐率</a></td>';
//					html += '<td><a href="javascript:void();" onclick="window.location.href=\'bloodPoolChanges.php?serverid='+datas[i].serverid+'\'">血池变化</a></td>';
//					html += '</tr>';
					html += '</table></td>';

					html += "</tr>";
				}
			}

			//统计数据
			var countInfo = msg.countInfo;
			//console.log(countInfo.drtfl);
			//return;
			$("#xtdqxc").html(countInfo.xtdqxc==""?0:countInfo.xtdqxc);//系统当前血池
			$("#zsy").html(countInfo.zsy==""?0:countInfo.zsy);//总输赢
			$("#drsy").html(countInfo.drsy==""?0:countInfo.drsy);//当日输赢
			$("#drcf").html(countInfo.drcf==null?0:countInfo.drcf);//当日吃分
			$("#drtf").html(countInfo.drtf==null?0:countInfo.drtf);//当日吐分
			$("#drtfl").html(doubleFormat(countInfo.drtfl==null?0:countInfo.drtfl));//当日吐分率

			$("#databody").html(html);
		},
		error: function(msg){
			alert("连接出错！");
		}
	});
}

//查询所有
function loadAllData(){
	$("#reset").click();

	loadDatas();
	
	$("#room").html("<option value=''>&nbsp;&nbsp;全部</option>");
}

//打开添加血池控制弹框
function addXueChi(serverid, roomname, xcph, syxc){
	$("#serverId").val(serverid);
	$("#roomname").html(roomname);
	$("#xcphz").html(xcph);
	$("#dqxcz").html(syxc);
	$("#add_xuechi").modal("show");
}
//修改血池税率
function changeXueChi(serverid, roomname, xcph){
	$("#condition").val(serverid);
	$("#roomname_change").html(roomname);
	$("#dqxcz_").html(xcph);
	$("#change_xuechi").modal("show");
}

//添加血池控制数据提交
function addRoomSet(){
	var serverid = $("#serverId").val();
	var setTime = $("#doingTime").val();
	var score = $("#score").val();
//	if(setTime == ""){
//		$("#doingTime").css("border","1px solid red");
//		$("#dtError").css("display","block");
//	}else{
//		$("#doingTime").css("border","");
//		$("#dtError").css("display","none");
//	}
	if(score == "" || isNaN(score)){
		$("#score").css("border","1px solid red");
		$("#sError").css("display","block");
	}else{
		$("#score").css("border","");
		$("#sError").css("display","none");
	}
	
	if(score!="" && (!isNaN(score))){
//		alert("serverid: "+serverid+"; setTime:" +setTime+"; score:"+score);
		$.ajax({
			type: "post",
			data: {"serverid":serverid, "setTime":setTime, "score":score},
			url: "/admin/roomset/addroomset",
			beforeSend: function(){
				$("#load").css("display","block");
			},
			success: function(msg){
				$("#load").css("display","none");
//				alert(msg);
				if(msg == "1"){
					$("#add_xuechi").modal("hide");
					$("#tishi").modal("show");
					$("#load").css("display","block");
				}else{
					alert(msg);
				}
			}
		});
	}
}
//修改血池
function changexc(){
	var serverid = $("#condition").val();
	// var setTime = $("#doingTime").val();
	var score = $("#xc_rate").val();
	if(score == "" || isNaN(score)){
		$("#xc_rate").css("border","1px solid red");
		$("#sError").css("display","block");
	}else{
		if(score<=10000&score>0){
			$("#xc_rate").css("border","");
			$("#sError").css("display","none");
			if(score!="" && (!isNaN(score))){
				// alert("serverid: "+serverid+";  score:"+score);
				$.ajax({
					type: "post",

					data: {"serverid":serverid,"xc_rate":score},
					url: "/admin/roomset/changebloodrate",
					beforeSend: function(){
						$("#load").css("display","block");
					},
					success: function(msg){
						$("#load").css("display","none");
						if(msg == "1"){
							$("#change_xuechi").modal("hide");
							$("#tishi").modal("show");
							$("#load").css("display","block");
						}else{
							alert(msg);
						}
					},
					error:function () {
						alert('no');
					}
				});
			}
		}else{
			$('#msg').html('血池税率不在0到10000之间');
		}
	}

}

//取消弹框
function cancelAdd(){
	$("#doingTime, #score").css("border","");
	$("#dtError, #sError").css("display","none");
	$("#add_xuechi").modal("hide");
}
//取消弹框血池税率修改
function cancelchange(){
	$("#doingTime, #score").css("border","");
	$("#dtError, #sError").css("display","none");
	$("#change_xuechi").modal("hide");
}